import org.junit.Test;

import java.io.*;

/**
 * @author 35326
 * 2021/3/23 13:43
 * File类的使用
 */
public class FileTest {
	public static void main(String[] args) {
//		File file = new File("file.txt");
//		boolean newFile = false;
//		try {
//			newFile = file.createNewFile();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		System.out.println(newFile);
		FileTest fileTest = new FileTest();
		fileTest.test2();
	}

	@Test
	public void test1() {
		File file = new File("file1.txt");
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fileWriter.write("34");
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (fileWriter != null) {
			try {
				fileWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 测试标准输入输出流
	 */
	@Test
	public void test2() {
		InputStream in = System.in;
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
		while (true) {
			String s = null;
			try {
				s = bufferedReader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (s.equalsIgnoreCase("e") || s.equalsIgnoreCase("exit")) {
				break;
			}
			System.out.println(s);
		}

		try {
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testObject() throws IOException, ClassNotFoundException {
	    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.obj",false));
	    oos.writeObject(new Person("你好"));
	    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("person.obj"));
		System.out.println((Person)objectInputStream.readObject());
	}
}

class Person implements Serializable{
	private static final long serialVersionUID = -6249326059038913066L;
	private String name;

	public Person(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				'}';
	}
}
